package com.assets.Facility_Maintenance;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.*;

import com.assets.Adapter.Scheme_Ap;
import com.assets.BaseActivity;
import com.assets.Ip_set.Url;
import com.assets.R;
import com.assets.bean.Scheme_bean;
import com.assets.util.TrustAllCerts;
import com.demo.WaveSwipeRefreshLayout;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.apache.http.conn.ssl.AllowAllHostnameVerifier;

import okhttp3.*;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class Scheme extends BaseActivity {
    private ImageView fh, add;
    private String url = "http://" + Url.IP + "/api/deviceErrordetailQuery";
    private ListView SM_list;
    private String remark, maintainId, errorId = "";
    private TextView title_text;
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();//全部
    WaveSwipeRefreshLayout mWaveSwipeRefreshLayout;

    @Override
    public int intiLayout() {
        return R.layout.scheme;
    }

    @Override
    public void initView() {
        Intent intent = getIntent();
        remark = intent.getStringExtra("remark");
        maintainId = intent.getStringExtra("maintainId");
        SM_list = (ListView) findViewById(R.id.SM_list);
        fh = (ImageView) findViewById(R.id.fh);
        title_text = (TextView) findViewById(R.id.title_text);
        add = (ImageView) findViewById(R.id.adds);
        title_text.setText("异常库");
    }

    @Override
    public void initData() {
        mWaveSwipeRefreshLayout = (WaveSwipeRefreshLayout) findViewById(R.id.main_swipe);
        mWaveSwipeRefreshLayout.setColorSchemeColors(Color.WHITE, Color.WHITE);//设置水滴中旋转动画的颜色，参数1:水滴落下过程中的颜色，参数2:水滴已经落下后的颜色
        mWaveSwipeRefreshLayout.setWaveColor(0xFF3794fc);//设置水滴样色
        mWaveSwipeRefreshLayout.setMaxDropHeight(1500);//设置最大下拉高度
        mWaveSwipeRefreshLayout.setOnRefreshListener(new WaveSwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {//模拟3秒的加载过程
                    @Override
                    public void run() {
                        load();
                        //接下来可以刷新ListView
                    }
                }, 1200);


            }

        });
        load();
        fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Scheme.this, Add_Scheme.class);
                intent.putExtra("maintainId", maintainId);
                intent.putExtra("state", "1");
                intent.putExtra("errorId", errorId);
                startActivity(intent);
                finish();
            }
        });
        SM_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(Scheme.this, Add_Scheme.class);
                intent.putExtra("errorId", String.valueOf(list.get(position).get("id")));
                intent.putExtra("maintainId", maintainId);
                intent.putExtra("state", "0");
                intent.putExtra("text", String.valueOf(list.get(position).get("text")));
                startActivity(intent);
                finish();
            }
        });
    }

    private void load() {
        new Thread() {
            @Override
            public void run() {
                try {
                    OkHttpClient client =new OkHttpClient
                .Builder()
                .hostnameVerifier(new AllowAllHostnameVerifier())
                .sslSocketFactory(TrustAllCerts.createSSLSocketFactory(), TrustAllCerts.getX509TrustManager())
                .connectTimeout(10, TimeUnit.SECONDS)//连接超时
                .readTimeout(500, TimeUnit.SECONDS)
                .build();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("remark", remark)
                            .build();
                    Request request = new Request.Builder()
                            .url(url)
                            .post(requestBody).build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Log.i("222", responseData.toString());
                    showResponse(responseData);

                } catch (IOException e) {
                    e.printStackTrace();

                }


            }
        }.start();
    }

    private void showResponse(final String responseData) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mWaveSwipeRefreshLayout.setRefreshing(false);//停止加载动画
                if (responseData.equals("[]")) {
                    Toast.makeText(Scheme.this, "无方案，请添加", Toast.LENGTH_SHORT).show();
                    return;
                }
                list.clear();
                Scheme_Ap scheme_ap1 = new Scheme_Ap(Scheme.this, list);
                SM_list.setAdapter(scheme_ap1);
                Gson gson = new Gson();
                List<Scheme_bean> userList = gson.fromJson(responseData, new TypeToken<List<Scheme_bean>>() {
                }.getType());
                for (int i = 0; i < userList.size(); i++) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("id_tv", "异常库ID：");
                    map.put("text_tv", "解决方案：");
                    map.put("date_tv", "创建时间:");
                    map.put("id", userList.get(i).getErrorId());
                    map.put("text", userList.get(i).getText());
                    map.put("date", userList.get(i).getDate());
                    list.add(map);
                }
                Collections.reverse(list);
                Scheme_Ap scheme_ap = new Scheme_Ap(Scheme.this, list);
                SM_list.setAdapter(scheme_ap);
            }
        });
    }
}
